﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace RentIt06WPFClient
{
    /// <summary>
    /// Interaction logic for EditMedia.xaml
    /// </summary>
    public partial class EditMedia : Page
    {
        private PublisherSession.Media media;

        public EditMedia(PublisherSession.Media media)
        {
            this.media = media;
            InitializeComponent();
            header.Content = header.Content.ToString() + media.Id;
            title.Text = media.Title;
            author.Text = media.Author;
            category.Text = media.Category;
            releaseDate.SelectedDate = media.ReleaseDate;
            rentingPrice.Text = media.RentingPrice.ToString();
            purchasePrice.Text = media.PurchasePrice.ToString();
            purchaseOptions.SelectedIndex = (media.CanBeBought ? 1 : 0) + (media.CanBeRented ? 2 : 0);
            fileName.Text = media.Filename;
            mediaType.SelectedIndex = (int)media.MediaType;
            isPublished.Value = (media.IsPublished ? 1 : 0);

            uploadButton.IsEnabled = !media.IsPublished;
        }

        private void updateUpdateButton()
        {
            decimal output;
            updateButton.IsEnabled = (releaseDate.SelectedDate.HasValue &&
                decimal.TryParse(rentingPrice.Text, out output) &&
                decimal.TryParse(purchasePrice.Text, out output) &&
                fileName.Text.Split('.').Length > 1);
        }

        private void updateMedia(object sender, RoutedEventArgs e)
        {
            media.Title = title.Text;
            media.Author = author.Text;
            media.Category = category.Text;
            if (releaseDate.SelectedDate.HasValue)
                media.ReleaseDate = releaseDate.SelectedDate.Value;
            media.RentingPrice = decimal.Parse(rentingPrice.Text);
            media.PurchasePrice = decimal.Parse(purchasePrice.Text);
            media.CanBeBought = (purchaseOptions.SelectedIndex == 1 || purchaseOptions.SelectedIndex == 3);
            media.CanBeRented = (purchaseOptions.SelectedIndex >= 2);
            media.Filename = fileName.Text;
            media.MediaType = (PublisherSession.MediaType) mediaType.SelectedIndex;

            var publishedBefore = media.IsPublished;
            media.IsPublished = isPublished.Value > 0.5;

            Session.Publisher.UpdateMedia(media);
            if (media.IsPublished && !publishedBefore)
                Session.Publisher.PublishMedia(media.Id);
            else if (!media.IsPublished && publishedBefore)
                Session.Publisher.UnPublishMedia(media.Id);

            uploadButton.IsEnabled = !media.IsPublished;
        }

        private void uploadMedia(object sender, RoutedEventArgs e)
        {
            new UploadWindow(media, (Button) sender);
        }

        private void cancel(object sender, RoutedEventArgs e)
        {
            NavigationService.GoBack();
        }

        private void dateValidation(object sender, SelectionChangedEventArgs e)
        {
            wrongDateImage.Visibility = this.releaseDate.SelectedDate.HasValue ?
                System.Windows.Visibility.Hidden :
                System.Windows.Visibility.Visible;
            updateUpdateButton();
        }

        private void rentingPriceChanged(object sender, TextChangedEventArgs e)
        {
            decimal output;
            this.wrongRentingPrice.Visibility = decimal.TryParse(this.rentingPrice.Text, out output) ?
                System.Windows.Visibility.Hidden :
                System.Windows.Visibility.Visible;
            updateUpdateButton();
        }

        private void purchasePriceChanged(object sender, TextChangedEventArgs e)
        {
            decimal output;
            this.wrongPurchasePrice.Visibility = decimal.TryParse(this.purchasePrice.Text, out output) ?
                System.Windows.Visibility.Hidden :
                System.Windows.Visibility.Visible;
            updateUpdateButton();
        }

        private void fileNameChanged(object sender, TextChangedEventArgs e)
        {
            this.wrongFileName.Visibility = this.fileName.Text.Split('.').Length > 1 ?
                System.Windows.Visibility.Hidden :
                System.Windows.Visibility.Visible;
            updateUpdateButton();
        }
    }
}
